Passed
Pull Request — master (#136)
by
unknown
01:52
created

ID3FrameBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 36
dl 0
loc 45
rs 10
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A getBuffer 0 6 1
A append 0 3 1
A appendNullTerminatedValue 0 5 1
A appendStaticValue 0 9 1
A appendStaticNumber 0 10 3
1
import * as ID3Util from "./ID3Util"
2
import { isString } from "./util"
3
import { TextEncoding } from "./definitions/Encoding"
4
5
type Value = Buffer | number | string
6
7
export class ID3FrameBuilder {
8
    private identifier: string
9
    private buffer = Buffer.alloc(0)
10
11
    constructor(identifier: string) {
12
        this.identifier = identifier
13
    }
14
15
    appendStaticValue(
16
        value: Value,
17
        size: number,
18
        encoding = TextEncoding.ISO_8859_1
19
    ) {
20
        const convertedValue = convertValue(value, encoding)
21
        this.append(staticValueToBuffer(convertedValue, size))
22
        return this
23
    }
24
25
    appendStaticNumber(value: number, size: number) {
26
        if(Number.isInteger(value)) {
27
            let hexValue = value.toString(16)
28
            if(hexValue.length % 2 !== 0) {
29
                hexValue = "0" + hexValue
30
            }
31
            this.append(staticValueToBuffer(Buffer.from(hexValue, 'hex'), size))
32
        }
33
        return this
34
    }
35
36
    appendNullTerminatedValue(value = '', encoding = TextEncoding.ISO_8859_1) {
37
        const convertedValue = convertValue(value, encoding)
38
        this.append(nullTerminatedValueToBuffer(convertedValue, encoding))
39
        return this
40
    }
41
42
    getBuffer() {
43
        const header = Buffer.alloc(10)
44
        header.write(this.identifier, 0)
45
        header.writeUInt32BE(this.buffer.length, 4)
46
        return Buffer.concat([header, this.buffer])
47
    }
48
49
    private append(buffer: Buffer) {
50
        this.buffer = Buffer.concat([this.buffer, buffer])
51
    }
52
}
53
54
function convertValue(value: Value, encoding = TextEncoding.ISO_8859_1) {
55
    if (value instanceof Buffer) {
56
        return value
57
    }
58
    if(Number.isInteger(value) || isString(value)) {
59
        return ID3Util.stringToEncodedBuffer(value.toString(), encoding)
60
    }
61
    return Buffer.alloc(0)
62
}
63
64
function staticValueToBuffer(buffer: Buffer, size: number) {
65
    if(!(buffer instanceof Buffer)) {
66
        return Buffer.alloc(0)
67
    }
68
    if(size && buffer.length < size) {
69
        return Buffer.concat([Buffer.alloc(size - buffer.length, 0x00), buffer])
70
    }
71
    return buffer
72
}
73
74
function nullTerminatedValueToBuffer(buffer: Buffer, encoding: number) {
75
    return Buffer.concat([buffer, terminateBuffer(encoding)])
76
}
77
78
function terminateBuffer(encoding: number) {
79
    if (encoding === TextEncoding.UTF_16_WITH_BOM ||
80
        encoding === TextEncoding.UTF_16_BE
81
    ) {
82
        return Buffer.alloc(2, 0x00)
83
    }
84
    return Buffer.alloc(1, 0x00)
85
}
86
87